package com.example.config;

import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import com.zaxxer.hikari.util.DriverDataSource;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Author: 不是菜鸡爱编程
 * @Date: 2023/12/03/10:33
 * @Description:
 */
@Setter
@Getter
@ToString
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class FluentMyBatisConfig {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    /**
     * 设置dataSource属性
     *
     * @return
     */
    @Bean
    public DataSource dataSource() {
        return new DriverDataSource(url, driverClassName, new Properties(), username, password);
    }

    /**
     * 定义mybatis的SqlSessionFactoryBean
     *
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }

    @Bean
    public MapperFactory mapperFactory() {
        return new MapperFactory();
    }
}
